﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using CustomsSystem.Model;
namespace CustomsSystem.BLL
{
    /// <summary>
    /// info_form
    /// </summary>
    public partial class info_form
    {
        private readonly CustomsSystem.DAL.info_form dal = new CustomsSystem.DAL.info_form();
        public info_form()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.info_form model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.info_form model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            return dal.Delete(ID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.info_form GetModel(int ID)
        {

            return dal.GetModel(ID);
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CustomsSystem.Model.info_form> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CustomsSystem.Model.info_form> DataTableToList(DataTable dt)
        {
            List<CustomsSystem.Model.info_form> modelList = new List<CustomsSystem.Model.info_form>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CustomsSystem.Model.info_form model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
        #region 导出Excel的方法
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="id">模板ID</param>
        /// <param name="sheetID">委托单编号，可以空，可以多个用逗号隔开</param>
        /// <param name="newpath">文件地址</param>
        /// <param name="date">日起范围between stardate and enddate</param>
        public void Exesql(int id, string sheetID, string newpath, string date)
        {
            string colIndex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//为了转换成数值
            ModelToExcel excel = new ModelToExcel();

            DataTable dt = dal.GetList(" FID= " + id).Tables[0];
            //没有委托单号的时候
            if (sheetID == "")
            {
                sheetID = "1";//为空，即为合计，合计只有一条数据，需要一个长度，不然下面出错
            }

            string[] sheetid = sheetID.Split(',');//委托单编号
            //汇总的数据
            string[] strRow = new string[sheetid.Length];//行
            string[] strCol = new string[sheetid.Length]; ;//列
            string[] strValues = new string[sheetid.Length];//值

            if (dt != null)
            {
                int x = 0;//用于数据的下标
                foreach (string i in sheetid)
                {
                    string row = "";
                    string rowName = "";
                    string col = "";//存列名
                    string colName = "";//全部列名
                    string sql = "";//sql的结果
                    string values = "";//全部的sql结果
                    foreach (DataRow dr in dt.Rows)
                    {
                        string tempcol = dr[2].ToString();
                        sql = dr[3].ToString();
                        if (sql.Trim() != "")
                        {
                            sql = Tosql(sql, i, date);//
                            values += dal.ExeSQL(sql) + ",";//执行sql后的值
                        }
                        if (tempcol.Trim() != "")
                        {
                            row = tempcol.Substring(1);
                            rowName += row + ",";//行的值
                            col = (colIndex.IndexOf(tempcol.Substring(0, 1).ToUpper()) + 1).ToString();
                            colName += col + ",";//列的值
                        }
                    }
                    strRow[x] = rowName.Substring(0,rowName.Length-1);//用数组存值
                    strCol[x] = colName.Substring(0, colName.Length - 1);
                    strValues[x] = values.Substring(0, values.Length - 1);
                    x++;
                }
            }
            

            excel.OutputExcel(strRow, strCol, strValues, newpath);
        }
        /// <summary>
        /// sql的转换
        /// </summary>
        /// <param name="sql">未转换的sql</param>
        /// <param name="id">委托单号</param>
        /// <param name="date">日起范围</param>
        /// <returns>转化好的sql</returns>
        public string Tosql(string sql, string id, string date)
        {
            sql = sql.Replace("@fee@", " sfeeAmount ");//单项费用
            sql = sql.Replace("@feesum@", " SUM(sfeeAmount) ");//费用合计
            sql = sql.Replace("@income@", " T_bill ");//应收表
            sql = sql.Replace("@pay@", " T_bill ");//应付表
            sql = sql.Replace("@rincome@", " T_pay ");//实收表
            sql = sql.Replace("@rpay@", " T_pay ");//实付表
            sql = sql.Replace("@sheetid@", " sheetid=" + id);//单号
            sql = sql.Replace("@check@", " auditEmpID='1'  ");//审核
            sql = sql.Replace("@nocheck@", " auditEmpID='1'");//不审核
            sql = sql.Replace("@feetype@", " feetypeid ");//费用类别
            
            sql = sql.Replace("@sheetInfo@", "T_sheetInfo");//委托单表
            sql = sql.Replace("@shipname@", "shipName");//船名
            sql = sql.Replace("@goodsname@", "goodsName");//货名
            sql = sql.Replace("@No@", "No");//数量
            sql = sql.Replace("@contractNo@", "contractNo");//合同号
            sql = sql.Replace("@PO@", "PO");//订单号
            sql = sql.Replace("@agencyDate@", "convert(varchar(10),agencyDate,120)");//进口日期
            if(date!="")//没日起不执行
            sql = sql.Replace("@date@", " sheetid in(select sheetid from T_sheetinfo where 1=1 " + date+")");//日起范围，用于合计
            return sql;
        }
        #endregion
    }
}

